package com.junxonline.instant.admin.controller.file;

import com.junxonline.instant.dao.entity.vo.file.UploadFileVO;
import com.junxonline.instant.admin.service.file.UploadFileService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文件上传Controller
 *
 * @author JunX
 * @date 2021-12-10
 */
@Log4j2
@RestController
@RequestMapping(value = "/file/uploadFile")
public class UploadFileController {

    @Autowired
    private UploadFileService uploadFileService;

    /**
     * 获取临时上传地址
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @OperationLog(label = "文件上传-获取临时上传地址", type = OperationEnum.QUERY)
    @PostMapping(value = "/getSignedUploadFileUrl")
    public CommonResult getSignedUploadFileUrl(@RequestBody(required = false) UploadFileVO vo) {
        if (vo.getBucketName() == null || vo.getOriginName() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(uploadFileService.getSignedUploadFileUrl(vo));
    }

    /**
     * 获取临时上传地址（原始文件名）
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @OperationLog(label = "文件上传-获取临时上传地址(原始文件名)", type = OperationEnum.QUERY)
    @PostMapping(value = "/getSignedUploadFileUrlOriginName")
    public CommonResult getSignedUploadFileUrlOriginName(@RequestBody(required = false) UploadFileVO vo) {
        if (vo.getBucketName() == null || vo.getOriginName() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(uploadFileService.getSignedUploadFileUrlOriginName(vo));
    }

}
